/*
 * Copyright 2000-2020 ALPHA LAB.Inc All Rights Reserved.
 */
package com.alphalab.moonlight.demo.dao.internal;
 
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; 
import org.springframework.stereotype.Repository;
import com.alphalab.framework.dao.GeneralQueryDao;
import com.alphalab.framework.util.StringUtil;
import com.alphalab.moonlight.demo.dao.model.Airline;
import com.alphalab.moonlight.demo.dao.model.PassengerTravel;
import com.alphalab.moonlight.demo.dao.service.IAirlineQueryDao;
import com.alphalab.moonlight.demo.service.model.FlightQueryParamVO; 

/**
 * AirlineQueryDao.
 * @author allen 
 * @version 1.0.0 2018年2月8日
 */
@Repository
public class AirlineQueryDao extends GeneralQueryDao<Airline> implements IAirlineQueryDao {

	/**
	 * 根据航班查询条件对象VO分页查询航班结果集.
	 * @param flightQueryParamVO 航班查询条件对象VO
	 * @param pageIndex 第几页，0表示不分页
	 * @param pageSize 每页的记录数，0表示不分页
	 * @return List<Airline>
	 */
	@Override
	public List<Airline> getAirlineList(final FlightQueryParamVO flightQueryParamVO,
			final int pageIndex, final int pageSize) {
		final Map<String, Object> paramMap = new HashMap<String, Object>(); 
		//由于有多个方法共用，所以这里的全部参数需判空 
		//单表查询不用hibernate提供的criteria，而通过拼接sql查询
		String condition = " WHERE 1=1 ";
		if (StringUtil.isNotEmptyString(flightQueryParamVO.getDeparture())) {
			condition += " AND DA.DEPARTURE = :DEPARTURE ";
			paramMap.put("DEPARTURE", flightQueryParamVO.getDeparture());
		}
		if (StringUtil.isNotEmptyString(flightQueryParamVO.getDestination())) {
			condition += " AND DA.DESTINATION = :DESTINATION ";
			paramMap.put("DESTINATION", flightQueryParamVO.getDestination());
		}	
		if (StringUtil.isNotEmptyString(flightQueryParamVO.getBeginTime()) 
				&& StringUtil.isNotEmptyString(flightQueryParamVO.getEndTime())) {
			condition += " AND DA.BEGIN_DATE BETWEEN :BEGINTIME AND :ENDTIME ";
			paramMap.put("BEGINTIME", flightQueryParamVO.getBeginTime());
			paramMap.put("ENDTIME", flightQueryParamVO.getEndTime());
		}		
		if (StringUtil.isNotEmptyString(flightQueryParamVO.getFlights())) {
			condition += " AND DA.FLIGHT_ID IN (:FLIGHTS) "; 
			paramMap.put("FLIGHTS", flightQueryParamVO.getFlights().split(","));			
		} 
		final String sql = MessageFormat.format(AirlineQueryDaoConst.SQL_GET_AIRLINELIST_BY_QUERYPARAM, 
				new Object[]{condition}); 
		List<Airline> airlineList = new ArrayList<Airline>();
		if (pageIndex > 0 && pageSize > 0) {
			airlineList = super.findBySql(sql, paramMap, Airline.class, pageIndex, pageSize); 
		} else {
			airlineList = super.findBySql(sql, paramMap, Airline.class); 
		} 
		return airlineList;
	}

	/**
	 * 根据航班查询条件对象VO查询旅客行程结果集.
	 * @param flightQueryParamVO 航班查询条件对象VO
	 * @return List<PassengerTravel>
	 */
	@Override
	public List<PassengerTravel> getPassengerTravelList(final FlightQueryParamVO flightQueryParamVO) {
		final Map<String, Object> paramMap = new HashMap<String, Object>();
		//由于前端传递的是固定值，故这里参数均不判空
		paramMap.put("DEPARTURE", flightQueryParamVO.getDeparture());
		paramMap.put("BEGINTIME", flightQueryParamVO.getBeginTime());
		paramMap.put("ENDTIME", flightQueryParamVO.getEndTime()); 
		final List<PassengerTravel> passengerTravelList = super.findBySql(
				AirlineQueryDaoConst.SQL_GET_PASSENGERTRAVEL_LIST_BY_QUERYPARAM,
				paramMap, PassengerTravel.class); 
		return passengerTravelList;
	}

}
